package com.zhizuobiao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 注解使用
 */
@Controller
@RequestMapping("/user")
public class AnnoController {

    // /user + /sv
    @RequestMapping("/sv")
    public ModelAndView simpleView() {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("simple");
        mv.addObject("message", "simpleView");

        return mv;
    }

    // /user + /sv2
    @RequestMapping("/sv2") // 默认赋值给value属性，"/sv2" --> value
    public ModelAndView simpleView2() {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("simple");
        mv.addObject("message", "222simpleView");

        return mv;
    }

    @RequestMapping(value = "/sv3", method = RequestMethod.POST)
    public ModelAndView simpleView3() {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("simple");
        mv.addObject("message", "222simpleView");

        return mv;
    }

    @RequestMapping(value = "/sv4")
    public void simpleView4(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("{name:admin,age:12}");
    }

    /**
     * 转发方式一
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/sv5")
    public void simpleView5(HttpServletRequest request, HttpServletResponse response) throws Exception {

        request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response);
    }

    /**
     * 转发方式二
     */
    @RequestMapping(value = "/sv6")
    public String simpleView6() {

        return FORWARD + "/WEB-INF/jsp/hello.jsp";
    }

    private static final String FORWARD = "forward:";
    private static final String REDIRECT = "redirect:";

    /**
     * 重定向springmvc支持写法
     */
    @RequestMapping(value = "/sv7")
    public String simpleView7() {

        return REDIRECT + "http://www.baidu.com";
    }


    //指定逻辑视图名，经过视图解析器解析为jsp物理路径：/WEB-INF/jsp/simple.jsp
    @RequestMapping(value = "/sv8")
    public String simpleView8() {
        return "simple";
    }

    /**
     * 返回字符串内容--浏览器
     *
     * @return
     */
    @RequestMapping(value = "/sv9")
    @ResponseBody // 添加注册，代表返回值，直接返回浏览器
    public String simpleView9() {
        return "simple";
    }


    // ModelAndView --> Model
    @RequestMapping(value = "/sv10")
    public String simpleView10(Model model, HttpSession session) {
        String sid = session.getId();

        model.addAttribute("message", sid);

        return "simple";
    }

    @RequestMapping("/sv11")
    public String simpleView11(Date today, Model model) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        model.addAttribute("message", sdf.format(today));
        return "simple";
    }
}
